package com.zt.securityTest.entity;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * time: 2024.11.15 17:34
 */
@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private User user;

    //权限集合
    private List<String> list;
    public LoginUser(User user,List<String> list) {
        this.user = user;
        this.list = list;
    }

    //自定义一个权限列表的集合,中转,不用序列化
    @JSONField(serialize = false)
    List<SimpleGrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        if (authorities != null){
            return authorities;
        }
        //将接收到的权限信息进行返回
        authorities = new ArrayList<>();
        authorities = list.stream().map(SimpleGrantedAuthority::new).toList();
        return authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    //账号是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //账号是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //账号是否未超时
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //账号是否可用
    @Override
    public boolean isEnabled() {
        return true;
    }
}
